///操作select控件
function createOption(value, text) {
     var opt = document.createElement("option");  
     opt.setAttribute("value", value);                                 
     opt.appendChild(document.createTextNode(text));                   
     return opt;
}
function addOptions(element,optionsInfo){
	for (var o in optionsInfo) { 
	     element.appendChild(createOption(o, optionsInfo[o]));
	     element.style.width="100%";
	     element.options[0].value="";
	     element.options[0].text="请选择";
    }
}
function addOptionsNo(element,optionsInfo){
	for (var o in optionsInfo) { 
	     element.appendChild(createOption(o, optionsInfo[o]));
    }
}
function clearOptions(obj) {
	obj.length = 1;  
	obj.options[0].selected = true;     
}
/////
function ajax_UpdateSQL(table,field,where){
	var updateSql = "";
	var form = document.forms[0];
	var fields = field.split(",");
	var wheres = where.split(",");
	var firstField  = eval("form."+fields[0]);
	if(firstField.length){
		for(l=0;l<firstField.length;l++){
			var sql = "";
			var value = "";
			var valueWhere = "";
			sql =  "update "+table+" set "
			//循环便利更新数值
			for(var i=0;i<fields.length;i++){
				var thisField = eval('form.'+fields[i]);
				value += fields[i] + " = ";
				if(thisField.length){
					 value  += "'"+thisField[l].value+"'";
				}else{
				 	 value  += "'"+thisField.value+"'";
				}
				if(value!="")
				value+=",";
			}
			//删除数值最后的，号 然后sql赋值
			sql += value.substring(0,value.length-1);
			//判断是否添加where条件
			if(where!=""&&where!=null){
				sql += " where ";
				//循环便利更新条件
				for(var i=0;i<wheres.length;i++){
					var thisField = eval('form.'+wheres[i]);
					valueWhere += wheres[i] + " = ";
					if(thisField.length){
						 valueWhere  += "'"+thisField[l].value+"'";
					}else{
					 	 valueWhere  += "'"+thisField.value+"'";
					}
					if(valueWhere!="")
					valueWhere+=" and ";
				}
				sql += valueWhere.substring(0,valueWhere.length-4);
			}
			if(updateSql!="")
			updateSql+="<><>";
			updateSql+=sql;
		}
	}else{
		updateSql = ajax_UpdateOne(table,field,where);
	}
	return updateSql;
}
function ajax_UpdateOne(table,field,where){
		
	var updateSql = "update "+table+" set ";
	
	var fields = field.split(",");
	
	var valueSql = "";
	for(i=0;i<fields.length;i++){
		if(valueSql!="")valueSql+=" , ";
		valueSql+=fields[i];
		valueSql+=" = ";
		valueSql+= "'"+document.forms[0][fields[i]].value+"'";
	}
	
	var wheres = where.split(",");
	
	var whereSql = "";
	for(i=0;i<wheres.length;i++){
		if(whereSql!="")whereSql+=" and ";
		whereSql+=wheres[i];
		whereSql+=" = ";
		whereSql+="'"+document.forms[0][wheres[i]].value+"'";
	}
	
	return updateSql+valueSql+" where "+whereSql; 
}
/**
 * @param table   数据库表名
 * @param column  所要赋值的控件name名.如查询多个列名,之间用逗号分隔.如a,b
 * @param arg0    where条件列名
 * @param arg1    是否自动生成SQL ''自动生成
 * @param flag    判断返回值为文本框,还是下拉框 true 表示文本框,false 表示下拉框
 * 
 * */
function doAjax(table,column,arg0,arg1,flag){	

	var array = doAjaxCom(table,column,arg0);	
			
	if(column.indexOf(",") == -1){
		var oC = document.getElementById(column);
		if(flag){
			ajax.doObj(table,column,array[0],array[1],arg1,flag,false,function(value){
									oC.value = value;												
								})
		}else{

			clearOptions(oC);
			
			ajax.doObj(table,column,array[0],array[1],arg1,flag,false,function(optionsInfo){
								
								addOptions(oC,optionsInfo)
								})			
		}
	}else{
		
		ajax.doObj(table,column,array[0],array[1],arg1,false,false,function(jso){							
							var vColumn = column.split(",");
							for(var i=0;i<vColumn.length;i++){
									var vCN = document.getElementById(vColumn[i]);
									vCN.value = eval("jso."+vColumn[i]);
									if(vCN.value == "undefined"){
										vCN.value ="";
									}					
							}
							
						});		
	}
}

/**
 * 添加行的ajax筛选
 * @param table   数据库表名
 * @param column  所要赋值的控件name名.如查询多个列名,之间用逗号分隔.如a,b
 * @param arg0    where条件列名
 * @param arg1    是否自动生成SQL ''自动生成
 * @param obj     所选控件的对象值
 * @param org     控件列下标
 * */
function doAjaxs(table,column,arg0,arg1,obj,org){	

	var array = doAjaxCom(table,column,arg0);	
	var objA = [obj.value];
	array[0] = objA;
	ajax.doObj(table,column,array[0],array[1],arg1,false,true,function(jso){							
							var orgs = org.split(",");
							if(orgs.length){
								for(i = 0;i<orgs.length;i++){
									var og = obj.parentNode.parentNode.childNodes[orgs[i]].firstChild;
									og.value = eval("jso."+og.name);
									if(og.value == 'undefined'){
										og.value = "";
									}
								}
							}
						})	
}
function doAjaxValues(table,column,arg0,arg1,obj,org){	

	var array = doAjaxCom(table,column,arg0);	
	var objA = [obj.value];
	ajax.doObj(table,column,array[0],array[1],arg1,false,true,function(jso){							
							var orgs = org.split(",");
							if(orgs.length){
								for(i = 0;i<orgs.length;i++){
									var og = obj.parentNode.parentNode.childNodes[orgs[i]].firstChild;
									og.value = eval("jso."+og.name);
									if(og.value == 'undefined'){
										og.value = "";
									}
								}
							}
						})	
}

/**
 * @param table   数据库表名
 * @param column  所要赋值的控件name名
 * @param arg0    where条件列名
 * @param arg1    是否自动生成SQL ''自动生成
 * 
 * */
function doAjaxCom(table,column,arg0){	
	var arrayV = [];
	var arrayN = [];
	var arrayCom = [];
	var vLen = arg0.split("-");

	if(vLen.length > 0){
		for(var i=0; i< vLen.length;i++){
			var vN = vLen[i];
			var vV = document.getElementById(vN).value;		
			arrayV[i]=vV;
			arrayN[i]=vN;
		}
	}else{
		var vV = document.getElementById(arg0).value;		
		arrayV[i]=vV;
		arrayN[i]=arg0;
	}
	arrayCom[0] = arrayV;
	arrayCom[1] = arrayN;
	return arrayCom;
}

function clearValue(){
	with(document.form[0]){
		
	}
}

//给文本框赋值
function loadingText(field,sql){
	field.value = "";
	ajax.loadingText(sql,function(str){
				field.value = str;
			})
}
//给文本框赋值 (控件,sql指向,where条件)
function loadingTexts(field,sql,where){
	field.value = "";
	ajax.loadingTexts(sql,where,function(str){
				field.value = str;
			})
}
//给下拉框赋值 (控件,sql指向,where条件)
function loadingSelectwhere(field,sql,where){
	clearOptions(field);
	ajax.loadingSelects(sql,where,function(str){
		var strs = str.split(",");
		for(i=0;i<strs.length;i++){
			if(strs[i]!=""){
			 	field.appendChild(createOption(strs[i],strs[i]));
		    	field.options[0].value="";
		   	 	field.options[0].text="请选择";
	   	 	}
		}
	});
}
//给下拉框赋值 (控件,sql指向,where条件) value 与 text不同的
function loadingSelectwherevt(field,sql,where){
	clearOptions(field);
	ajax.loadingSelectvt(sql,where,function(str){
		var strz = str.split("<>");
		var strs0 = strz[0].split(",");
		var strs1 = strz[1].split(",");
		for(i=0;i<strs0.length;i++){
			if(strs0[i]!=""){
			 	field.appendChild(createOption(strs0[i],strs1[i]));
		    	field.options[0].value="";
		   	 	field.options[0].text="请选择";
	   	 	}
		}
		//field.style.width = "99%";
	});
}
//给下拉框赋值
function loadingSelect(field,sql){
	clearOptions(field);
	ajax.loadingSelect(sql,function(str){
			var strs = str.split(",");
			for(i=0;i<strs.length;i++){
			 field.appendChild(createOption(strs[i],strs[i]));
	     	 field.style.width="100%";
	     	 field.options[0].value="";
	    	 field.options[0].text="请选择";
			}
			});
}
//给下拉框赋值
function loadSelect(field,sql){
	clearOptions(field);
	ajax.loadingSelect(sql,function(str){
			var strs = str.split(",");
		if(str!=""&&str!=null){
			for(i=0;i<strs.length;i++){
				 field.appendChild(createOption(strs[i],strs[i]));
		     	 field.style.width="100%";
		     	 field.options[0].value="";
		    	 field.options[0].text="请选择";
			}
		}else{
			return false;
		}
	})
}


//给多个文本赋值
//field 	= 	this
//arg 		= 	所要赋值的文本框的数组下标
//sql 		= 	 查询语句
function loadingJSONObject(field,arg,sql){
var args = arg.split(",");
var column="";
	if(arg.indexOf(",")!=-1){
		for(i = 0;i<args.length;i++){
			if(i!=0)column+=",";
			column+= field.parentNode.parentNode.childNodes[args[i]].firstChild.name;
			}
	}else{
	column = field.parentNode.parentNode.childNodes[arg].firstChild.name;
	}
	ajax.loadingJSONObject(sql,column,function(json){
				
							if(arg.indexOf(",")!=-1){
								for(i = 0;i<args.length;i++){
									var og = field.parentNode.parentNode.childNodes[args[i]].firstChild;
									og.value = eval("json."+og.name);
									if(og.value == 'undefined'){
										og.value = "";
									}
								}
							}
							else{
								var og = field.parentNode.parentNode.childNodes[arg].firstChild;
								og.value = eval("json."+og.name);
								if(og.value == 'undefined'){
									og.value = "";
								}
							}
			})
}


function getTime(flag){
	var thisDate = new Date();
	var yy = thisDate.getFullYear();
	var mm = thisDate.getMonth()+1;
	var dd = thisDate.getDate();
	var time = yy+flag+mm+flag+dd;
	return time;
}
/**
 * @param field		 下拉选对象
 * @param typechar   下拉选标识
 * @param give       默认内容值
 * @param choose     是否 带请选择空内容 默认true
 * @param renew      是否 清除原来内容 默认true
 **/
function doSelectInfo(field,typechar,give,choose,renew){
	choose = choose == undefined?true:choose;
	renew = renew == undefined ? true:renew;
	var bo = 0;
	if(typechar != "" && typechar != undefined){
		//判断是否清除原有内容
		if(renew){
			clearOptions(field);
		}
		DWREngine.setAsync(false);
		comdwr.doSelect_Info(typechar,function(optlist){
			if(optlist!=null){
				for(i=0;i<optlist.length;i++){
					var opt = createOption(optlist[i].selectvalue,optlist[i].selectvalue);
					//判断默认值选中
					if(give!=""&&give == optlist[i].selectvalue){
						bo++;
						opt.setAttribute("selected", "selected");
					}
					field.appendChild(opt);
			     	//判断是否带空内容
			     	if(choose){
						field.options[0].value="";
			    		field.options[0].text="请选择";
			     	}
				}
				if(!choose){
					field.removeChild(field.options[0]);
			    }
			}else{
				return false;
			}
		});
	}
	this.doElse = function(){
		field.attachEvent("onchange",new Function("elseChange('"+give+"',"+field+");"));
		var opt = createOption("其他","其他");
		field.appendChild(opt);
		if(give!=""&&bo==0){
			field.value = "其他";
			elseChange(give,field);
		}
	}
}
function elseChange(overall,field){
	var par = field.parentNode;
	if(field.value == "其他"){
		field.style.width = "50%";
		var input = document.createElement("INPUT"); //创建文本框
		input.style.width = "50%"; //设置宽度
	    input.setAttribute("name", field.name);//将新input的name 赋值
	    field.setAttribute("name", ""); //将下拉选的name清空
	    if(overall) input.setAttribute("value", overall); //设置默认值
	    if(field.check){
	    	input.setAttribute("check", field.check); //设置验证
	    	if(field.check.indexOf("checkBlank")!=-1){
	    		input.setAttribute("class", "notnull"); //样式
	    	}
	    }
		par["appendChild"](input);
	}else{
		if(field.nextSibling && field.nextSibling.nodeName =="INPUT"){
			field.style.width = "95%";
		    field.setAttribute("name", field.nextSibling.name);//还原 name 赋值
			par["removeChild"](field.nextSibling);
		}
	}
}